﻿using HamcoWcs.Tools.Tibco.Entities;
using Microsoft.Extensions.Configuration;
using SqlSugar;
using System;
using System.IO;

namespace HamcoWcs.Tools.Tibco
{
    public partial class Program
    {
        public static IConfiguration Configuration { get; set; }
        public static ISqlSugarClient SQLCLIENT { get; set; }

        private static Action PRESS_KEY_QUIT = () =>
        {
            var key = Console.ReadKey();

            while (key.Key != ConsoleKey.Escape)
            {
                key = Console.ReadKey();
            }
        };

        private static ISqlSugarClient GetSqlSugarClient() => new SqlSugarScope(new ConnectionConfig()
        {
            DbType = DbType.MySql,
            IsAutoCloseConnection = true,
            InitKeyType = InitKeyType.Attribute,
            ConnectionString = Configuration["ConnectionStrings:MysqlString"],
        });

        static Program()
        {
            Console.Title = "Tibco Message Server";
            IConfigurationBuilder cfgBuilder = new ConfigurationBuilder()
               .SetBasePath(Directory.GetCurrentDirectory())
               .AddJsonFile("appsettings.json");
            Configuration = cfgBuilder.Build();

            SQLCLIENT = GetSqlSugarClient();
        }



        private static void PrintSend(string msg)
            => Print(msg, "Send", ConsoleColor.Green);


        private static void PrintRecv(string msg)
            => Print(msg, "Recv", ConsoleColor.Blue);


        private static void PrintError(string msg)
            => Print(msg, "Error", ConsoleColor.Red);


        private static double GetSpanDuration(MesMsgQueue msg)
            => msg.CreatedTime.Subtract(msg.LastUpdateTime).Duration().TotalSeconds;


        private static void Print(string msg, string flag, ConsoleColor color)
        {
            string time = DateTime.Now.ToString("[yyyy-MM-dd HH:mm:ss.ffffff]");
            Console.ForegroundColor = color;
            Console.WriteLine($"{time} #{flag}");
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine(msg);
        }
    }
}
